package com.spring.showcase.controllers;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spring.showcase.domain.User;
import com.spring.showcase.exceptions.CustomException;

@Controller
public class HomeController {

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String showHomePage() {
        return "home";
    }

    @RequestMapping(value = "/home/exception", method = RequestMethod.GET)
    public String throwException() {
        throw new CustomException("custom exception");
    }

    @RequestMapping(value = "/error")
    public String handle(HttpServletRequest request, Model model) {
        model.addAttribute("status", request.getAttribute("javax.servlet.error.status_code"));
        model.addAttribute("reason", request.getAttribute("javax.servlet.error.message"));
        return "error2";
    }

    @RequestMapping(value = "/post", method = RequestMethod.POST)
    public void post() {
    }

    @RequestMapping(value = "/matrix/{value}", method = RequestMethod.GET)
    public String getMatrixVariables(@PathVariable String value, @MatrixVariable String alma, @MatrixVariable String korte) throws IOException {
        System.out.println(value);
        System.out.println(alma);
        System.out.println(korte);
        return "redirect:/home";
    }

    @RequestMapping(value = "/matrix/{value}/second/{value2}", method = RequestMethod.GET)
    public String getMultipleMatrixVariables(@PathVariable String value, @PathVariable String value2,
            @MatrixVariable(value = "alma", pathVar = "value") String alma, @MatrixVariable(value = "korte", pathVar = "value") String korte,
            @MatrixVariable(value = "barack", pathVar = "value2") String barack) throws IOException {
        System.out.println(value);
        System.out.println(alma);
        System.out.println(korte);
        System.out.println(value2);
        System.out.println(barack);
        return "redirect:/home";
    }

    @RequestMapping(value = "/entity")
    public ResponseEntity<User> getResponse() {
        return new ResponseEntity<User>(new User("Chris"), HttpStatus.OK);
    }

}
